//
//  WorkReviewViewController.m
//  AI
//
//  Created by zjhc on 2018/1/5.
//  Copyright © 2018年 zjhc. All rights reserved.
//

#import "WorkReviewViewController.h"
#import "WorkReviewCell.h"
#import "UIImageView+WebCache.h"
#import "AFAppDotNetAPIClient.h"
#import "UrlManage.h"
#import <SVProgressHUD.h>
#import "MJExtension.h"
#import "AudioListModel.h"
#import "ErrorCorrectionViewController.h"


@interface WorkReviewViewController ()<UITableViewDelegate,UITableViewDataSource> {
    
    NSString *imageStr;
    NSDateFormatter *format;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIDatePicker *datePicker;
@property (strong,nonatomic) NSString *dateStr;
@property (strong,nonatomic) AudioListModel *audioListModel;
@property (strong,nonatomic) NSArray *records;
- (IBAction)dateChanged:(id)sender;


@end

@implementation WorkReviewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView reloadData];
    self.tableView.scrollEnabled =NO;
    
    NSDate *date = [NSDate date];
    format = [[NSDateFormatter alloc]init];
    [format setDateFormat:@"yyyy-MM-dd"];
    self.dateStr = [format stringFromDate:date];//没有用上的数据
    [self.datePicker setDate:date animated:YES];
    [self audioListService:date];
    self.automaticallyAdjustsScrollViewInsets = false;

}

- (NSString *)dataTOjsonString:(id)object {
    
    NSString *jsonString = nil;
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object
                                                       options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                         error:&error];
    if ( !jsonData ) {
        NSLog(@"Got an error: %@", error);
    } else {
        jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    return jsonString;
}

#pragma mark - 获取音频列表接口服务
//mj_objectWithKeyValues 可以将字典转model,这是Mjextion库的应用
-(void)audioListService:(NSDate *) date {
    NSString *tempDateStr = [format stringFromDate:date];
    NSDictionary * dict = @{@"pageNo":@"1",@"pageSize":@"10",@"createUserId":@"101",@"createDate":tempDateStr};
    NSString *jsonString = [self dataTOjsonString:dict];
    NSDictionary * mutableDic = @{@"access_token":[NSUserDefaultsUtil gettoken],@"params":jsonString};
    
    [AFAppDotNetAPIClient getAsynchronous:[UrlManage sharedManager].audioList parameter:mutableDic successBlock:^(NSDictionary *result, NSError *error) {
        
        if ([[result objectForKey:@"code"] isEqualToString:@"0"]) {
            self.audioListModel = [AudioListModel mj_objectWithKeyValues:result];
            self.records= self.audioListModel.data.records;
            [self.tableView reloadData];
        } else {
            [SVProgressHUD showErrorWithStatus:[result objectForKey:@"msg"]];
        }
    } errorBlock:^(NSError *error) {
         [SVProgressHUD showErrorWithStatus:@"网络错误"];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.records.count;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return  1;
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WorkReviewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"WorkReviewCell" forIndexPath:indexPath];
    
    cell.avatarImage.layer.masksToBounds = YES;
    cell.avatarImage.layer.cornerRadius =cell.avatarImage.frame.size.width/2;
    [cell.avatarImage sd_setImageWithURL:[NSURL URLWithString:imageStr]  placeholderImage:[UIImage imageNamed:@"avatar" ] options:SDWebImageRefreshCached];
    cell.name.text = @"黄晨露";
    Records *recordModel =self.records[indexPath.row];
    
    NSTimeInterval second = recordModel.createDate.longLongValue / 1000.0;
        // 时间戳 -> NSDate *
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:second];    //获取系统当前时间
    //用于格式化NSDate对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设置格式：zzz表示时区
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    //NSDate转NSString
    NSString *currentDateString = [dateFormatter stringFromDate:date];
    cell.dateTime.text = currentDateString;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ErrorCorrectionViewController * errorCorrectionVC = [self.storyboard instantiateViewControllerWithIdentifier:@"ErrorCorrectionViewController"];
    errorCorrectionVC.record = self.records[indexPath.row];
    [self.navigationController pushViewController:errorCorrectionVC animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (IBAction)dateChanged:(id)sender {
    UIDatePicker* control = (UIDatePicker*)sender;
    NSDate* _date = control.date;
    
    [self audioListService:_date];
}

-(void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
}
@end
